persistence.setNamespace("peiwan_weixin");
var peiwanWeixin = angular.module('peiwan_weixin', []);
peiwanWeixin.controller('LoginCtrl', [ "$scope", function(s) {
	s.ok = function() {
		var username = $("#username").val();
		var password = $("#password").val();
		if (username == "") {
			swal({
				title : "",
				text : "请输入用户名！",
				type : "error"
			});
			return;
		}
		if (password == "") {
			swal({
				title : "",
				text : "请输入密码！",
				type : "error"
			});
			return;
		}
		password = SparkMD5.hash(password);
		ajaxWeixin("users/login", {
			username : username,
			password : password
		}, function(data) {
			var gotoIndex = function() {
				location.href = "index.html";
			}
			var json = JSON.parse(data);
			if (json.status == 0) {
				persistence.put("session", json.extra);
				var user = json.entity;
				persistence.put("loginUser", user);
				if (user.isAudit == 1) {
					swal({
						title : "",
						text : "你已经是电竞教官，无需申请了。",
						type : "info"
					}, gotoIndex);
				} else {
					location.href = "peiwan_form.html";
				}
			} else if (json.status == 1) {
				swal({
					title : "",
					text : "账号密码错误",
					type : "error"
				});
			}
		});
	};
} ]);